package com.demo.test.service;

import com.demo.test.mapper.TestMapper;
import com.demo.test.model.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TransactionTestService {

    private static final Logger log = LoggerFactory.getLogger(TransactionTestService.class);

    @Autowired
    private TestService testService;

    @Autowired
    private TestMapper testMapper;

    @Transactional(rollbackFor = Exception.class)
    public void performNestedOperation(String operationType) throws Exception {
        System.out.println("执行嵌套业务操作: " + operationType);

        testService.save(new Test(4L, "ff"));
        // 创建不可变列表
        List<Test> list = List.of(new Test(2L, "a"), new Test(3L, "bccccccccccccccccccccccccc"), new Test(6L, "bcffcccccccccccccccccccccccc"));
        try {
           // testService.saveBranch(list);
            this.saveBranch2(list);
        } catch (Exception e) {
            log.info("异常信息: {}", e.getMessage());
        }
        testService.save(new Test(5L, "ttt"));
        System.out.println("end");
    }


    @Transactional(rollbackFor = Exception.class)
    public void saveBranch2(List<Test> test) {
        for(int i=0;i<test.size();i++){
            testMapper.insert(test.get(i));
        }
    }

}